package com.geovis.emergency.spd.biz.pc.wrapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanDetailService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanTaskService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDisasterEvent;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyOrg;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanDetail;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanTask;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.pc.pojo.enums.CheckStatusEnum;
import com.geovis.emergency.spd.entity.pc.pojo.enums.TaskStatusEnum;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillPlanDetailVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.StatisticsDutyOrgVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.StatisticsEventVO;
import com.geovis.emergency.spd.entity.system.constant.SystemFileBusinessConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileQueryDTO;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/6/30
 */
@AllArgsConstructor
public class PcDrillDisasterEventWrapper extends BaseEntityWrapper<PcDrillDisasterEvent, StatisticsEventVO>{

    private final ISystemFileService systemFileService;
    private final IPcDrillPlanDetailService pcDrillPlanDetailService;
    private final IPcDrillPlanTaskService pcDrillPlanTaskService;
    private final List<PcDrillDutyOrg> groupList;


    public static PcDrillDisasterEventWrapper build(ISystemFileService systemFileService,IPcDrillPlanDetailService pcDrillPlanDetailService,IPcDrillPlanTaskService pcDrillPlanTaskService,List<PcDrillDutyOrg> groupList) {
        return new PcDrillDisasterEventWrapper(systemFileService,pcDrillPlanDetailService,pcDrillPlanTaskService,groupList);
    }
    @Override
    public StatisticsEventVO entityVO(PcDrillDisasterEvent entity) {
        StatisticsEventVO statisticsEventVO = BeanUtil.toBean(entity,StatisticsEventVO.class);
        //灾害类型
        if (StrUtil.isNotBlank(statisticsEventVO.getType())){
            String dictValue = DictCache.getDictValue(PcDisasterConstant.DISASTER_TYPE, statisticsEventVO.getType());
            statisticsEventVO.setTypeName(dictValue);
        }
        //事件等级
        if (StrUtil.isNotBlank(entity.getLevel())){
            String dictValue = DictCache.getDictValue(PcDisasterConstant.EVENT_LEVEL, entity.getLevel());
            statisticsEventVO.setLevelName(dictValue);
        }
        //获取附件信息
        if (ObjectUtil.isNotNull(systemFileService)){
            List<SystemFile> fileList = systemFileService.getListById(entity.getId(),SystemFileBusinessConstant.TYPE_PC_DRILL_DISASTER_EVENT);
            statisticsEventVO.setFileList(fileList);
        }

        List<PcDrillPlanDetail> pcDrillPlanDetails = pcDrillPlanDetailService.list(
                Wrappers.lambdaQuery(PcDrillPlanDetail.class)
                        .eq(PcDrillPlanDetail::getPcDrillId,entity.getPcDrillId())
                        .eq(PcDrillPlanDetail::getPcDrillDisasterEventId,entity.getId())
        );
        List<PcDrillPlanDetailVO> pcDrillPlanDetailVOList = getDrillPlanDetailVOList(pcDrillPlanDetails);

        List<StatisticsDutyOrgVO> dutyList = Lists.newArrayList();
        for (PcDrillDutyOrg drillDutyOrg : groupList) {
            List<PcDrillPlanDetailVO> collect = pcDrillPlanDetailVOList.stream().filter(s -> s.getGroupId().equals(drillDutyOrg.getId())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(collect)) {
                StatisticsDutyOrgVO dutyOrgVO = BeanUtil.toBean(drillDutyOrg, StatisticsDutyOrgVO.class);
                dutyOrgVO.setDrillPlanDetailList(collect);
                dutyList.add(dutyOrgVO);
            }
        }
        statisticsEventVO.setDutyOrgList(dutyList);
        return statisticsEventVO;
    }
    public List<PcDrillPlanDetailVO> getDrillPlanDetailVOList(List<PcDrillPlanDetail> pcDrillPlanDetails) {
        return pcDrillPlanDetails.stream().map(pcDrillPlanDetail -> {
            PcDrillPlanDetailVO pcDrillPlanDetailVO = BeanUtil.toBean(pcDrillPlanDetail, PcDrillPlanDetailVO.class);
            //方案审核通过了，查看任务反馈状态
            if (CheckStatusEnum.PASS.getCode().equals(pcDrillPlanDetailVO.getStatus())) {
                PcDrillPlanTask pcDrillPlanTask = pcDrillPlanTaskService.getOne(
                        Wrappers.<PcDrillPlanTask>lambdaQuery()
                                .eq(PcDrillPlanTask::getPcDrillId, pcDrillPlanDetailVO.getPcDrillId())
                                .eq(PcDrillPlanTask::getPcDrillPlanId, pcDrillPlanDetailVO.getPcDrillPlanId())
                                .eq(PcDrillPlanTask::getPcDrillPlanDetailId, pcDrillPlanDetailVO.getId())
                                .eq(PcDrillPlanTask::getPcDrillDisasterEventId, pcDrillPlanDetailVO.getPcDrillDisasterEventId())
                                .orderByDesc(PcDrillPlanTask::getCreateTime)
                                .last("limit 1")
                );
                if (ObjectUtil.isNotNull(pcDrillPlanTask)) {
                    pcDrillPlanDetailVO.setFeedbackStatus(pcDrillPlanTask.getStatus());
                } else {
                    pcDrillPlanDetailVO.setFeedbackStatus(TaskStatusEnum.WAIT_COMMIT.getCode());
                }
            }
            else
            {
                //如果方案还没审核，则按方案的状态来设置
                pcDrillPlanDetailVO.setFeedbackStatus(pcDrillPlanDetail.getStatus());
            }
            return pcDrillPlanDetailVO;
        }).collect(Collectors.toList());
    }
}
